#!/bin/bash

# 定义报告文件
REPORT_FILE="linux_inspection_report.txt"
DATE=$(date)

# 清空报告文件
> $REPORT_FILE

# 打印日期和时间
echo "Linux System Inspection Report" >>$REPORT_FILE
echo "Date: $DATE" >>$REPORT_FILE
echo "=============================================" >>$REPORT_FILE

# 系统负载检查
echo "| Check Item | Result |" >>$REPORT_FILE
echo "|-----------|--------|" >>$REPORT_FILE
LOAD=$(uptime)
echo "| System Load | $LOAD |" >>$REPORT_FILE
echo "=============================================" >>$REPORT_FILE

# 磁盘使用情况检查
echo "| Check Item | Result |" >>$REPORT_FILE
echo "|-----------|--------|" >>$REPORT_FILE
DISK_USAGE=$(df -h)
echo "| Disk Usage | $DISK_USAGE |" >>$REPORT_FILE
echo "=============================================" >>$REPORT_FILE

# 内存使用情况检查
echo "| Check Item | Result |" >>$REPORT_FILE
echo "|-----------|--------|" >>$REPORT_FILE
MEMORY_USAGE=$(free -m)
echo "| Memory Usage | $MEMORY_USAGE |" >>$REPORT_FILE
echo "=============================================" >>$REPORT_FILE

# 网络状态检查
echo "| Check Item | Result |" >>$REPORT_FILE
echo "|-----------|--------|" >>$REPORT_FILE
NETWORK_STATUS=$(ifconfig)
echo "| Network Status | $NETWORK_STATUS |" >>$REPORT_FILE
echo "=============================================" >>$REPORT_FILE

# 进程监控
echo "| Check Item | Result |" >>$REPORT_FILE
echo "|-----------|--------|" >>$REPORT_FILE
TOP_PROCESSES=$(ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head)
echo "| Top Processes by Memory Usage | $TOP_PROCESSES |" >>$REPORT_FILE
echo "=============================================" >>$REPORT_FILE

# 发送邮件
SUBJECT="Linux System Inspection Report - $DATE"
TO_EMAIL="收件邮箱@example.com"
mail -s "$SUBJECT"  $TO_EMAIL < $REPORT_FILE



--------------------------------------------------------------------------
如果没有mail命令需要先下载，命令如下：
yum -y install mailx

下载后，需要在/etc/mail.rc文件末尾追加如下命令：
set from=发件邮箱0@163.com
set smtp=smtp.163.com
set smtp-auth-user=发件邮箱@163.com
set smtp-auth-password=密码/授权码
set smtp-auth=login